<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .main{
            overflow: hidden;
        }
        .item{
            float: left;
            padding: 5px;
            border: 1px solid black;
        }
        img{
            width: 300px;
        }
    </style>
</head>
<body>
    <div class="main"></div>
    <script>
        var url = "http://47.113.104.50:8000/more";

        function http(url){
            return new Promise((resolve,reject)=>{
                $.ajax({
                    type: "get",
                    url,
                    success:  res=> {
                        resolve(res);
                    }
                });
            })
        }
        function imgDeal(arr){
            arr.forEach(item=>{
                var html = `<div class="item">
                                <img src="${item.imageUrl}" alt="">
                            </div>`
                $(".main").append(html)
            })
        }
        function position(){
            var ww = $(window).width();
            var box = $(".item").outerWidth();
            var num = Math.floor(ww/box);
            var arr = [];
            $(".item").each((index,item)=>{
                if(index<num){
                    var height = $(item).outerHeight()
                    arr.push(height);
                }else{
                    // 1 获取最小高度
                    // 2 通过最小高度获取下标，在通过下标获取左边的距离
                    // 3 设置当前块的top 和 left
                    // 4 更新 取代掉最小高度
                    var minHeight = Math.min(...arr);
                    var minIndex = arr.indexOf(minHeight);
                    var offsetLeft = $(".item").eq(minIndex).offset().left;
                    $(item).css({position:"absolute",left:offsetLeft,top:minHeight});
                    arr[minIndex] = minHeight + $(item).outerHeight();
                }
            })
        }
        async function go(url){
            var data = await http(url);
            await imgDeal(data);
            position();
        }
        go(url)
    </script>
</body>
</html>